<template>
  <el-dialog title="新增角色" :visible="visible" @close="handleClose">
    <el-form ref="RoleFormRefs" label-width="100px" :model="roleData" :rules="roleRules">
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="roleData.name" />
      </el-form-item>
      <el-form-item label="启用">
        <el-switch v-model="roleData.state" size="mini" active-value="1" inactive-value="0" />
      </el-form-item>
      <el-form-item label="角色描述" prop="description">
        <el-input v-model="roleData.description" type="textarea" />
      </el-form-item>
      <el-form-item style="text-align: center;width: 650px;">
        <el-button type="primary" @click="onsubmit">确定</el-button>
        <el-button type="danger" @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { getAddRoleApi } from '@/api/role'

export default {
  props: {
    visible: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      roleData: {
        name: '',
        description: '',
        state: 0
      },
      roleRules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '角色描述不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    handleClose() {
      this.$emit('update:visible', false)
      this.$refs.RoleFormRefs.resetFields()
    },
    async onsubmit(name) {
      // 整体校验
      this.$refs.RoleFormRefs.validate()
      // 校验新增的名称不能一样
      //   if (this.roleData.name === name) {
      //     this.$message.error('该名称已存在~~~')
      //   }
      // 发送添加请求
      await getAddRoleApi(this.roleData)
      // 提示消息
      this.$message.success('新增角色成功')
      // 重新获取数据
      this.$emit('pushRoleData')
      // 重置数据
      this.cancel()
    },
    cancel() {
      // 重置数据
      this.$refs.RoleFormRefs.resetFields()
      this.$emit('update:visible', false)
    }
  }

}
</script>

<style scoped>

</style>
